package com.JZ.QM.controller;

import com.JZ.QM.common.entity.CombinationShareEntity;
import com.JZ.QM.common.utils.CombinationShareSingle;
import com.JZ.QM.common.utils.CommonResult;
import com.JZ.QM.service.CombinationShareService;
import com.JZ.QM.service.ProductLibraryInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author cyc
 * @email cyc@gmail.com
 * @date 2020/6/20 23:01
 */
@RestController
@RefreshScope
@Slf4j
@RequestMapping(value="/combinationShare")
public class CombinationShareController {

    @Resource
    CombinationShareService combinationShareService;
    @Resource
    ProductLibraryInfoService productLibraryInfoService;

    /**
     * 根据组合id获得组合产品信息
     * @param comBaseId 组合基本信息id
     * @return
     */
    @RequestMapping("/getCombinationShareInfo")
    public CommonResult getCombinationShareInfoById(Long comBaseId){
        //声明查询map映射
        Map<String,Object> queryMapper=new HashMap<>();

        //构造查询映射
        queryMapper.put("com_base_id",comBaseId);
        List<CombinationShareEntity> combinationShareEntities = combinationShareService.listByMap(queryMapper);
        if (combinationShareEntities.isEmpty()){
            return CommonResult.error(201,"该组合中没有产品").put("combinationShareLists",new ArrayList<>());
        } else {
             CombinationShareSingle[]  combinationShareSingles=new CombinationShareSingle[combinationShareEntities.size()];
             int combinationShareSinglesIndex=0;
            for (Iterator<CombinationShareEntity> it = combinationShareEntities.iterator(); it.hasNext(); ) {
                CombinationShareSingle combinationShareSingle = new CombinationShareSingle();
                CombinationShareEntity iter = it.next();
                combinationShareSingle.setProTsCode(iter.getProTsCode());
                combinationShareSingle.setProName(productLibraryInfoService.getProName(iter.getProTsCode()));
                combinationShareSingle.setComShareRadio(iter.getComShareRadio());
                combinationShareSingles[combinationShareSinglesIndex++]=combinationShareSingle;
            }
            return CommonResult.success().put("combinatiobShares",combinationShareSingles);
        }

    }

    /**
     * 通过组合的id获取组合的详细信息
     * @param comBaseId 组合基本信息id
     * @return
     */
    @RequestMapping("/getCombinationShareDetails")
    public CommonResult getCombinationShareNamesById(Long comBaseId){
        //组合的详细信息(组合中产品的名称和各个产品的比例)
        String combinatonDetail;

        combinatonDetail=combinationShareService.getCombinationDetail(comBaseId);
        return CommonResult.success().put("newComDetail",combinatonDetail);

    }

    /**
     * 通过组合id获取成分产品列表
     * @param comBaseId
     * @return
     */
    @RequestMapping("/getCombinationShareList")
    public CommonResult getCombinationShareList(Long comBaseId){

        //股票成分产品列表
        ArrayList<CombinationShareEntity> stockList = new ArrayList<>();
        //基金成分产品列表
        ArrayList<CombinationShareEntity> fundList = new ArrayList<>();

        stockList = combinationShareService.getCombinationShareList(comBaseId,1);

        fundList = combinationShareService.getCombinationShareList(comBaseId,0);

        return CommonResult.success()
                .put("stockList",stockList)
                .put("fundList",fundList);
    }
}
